﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class POListForStock : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
            }
        }
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = " and CheckStatus='1' and IsClose='0' ";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and (PONO like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (txtAccessoryCompanyNum.Text.Trim() != "")
            {
                Whereclause += " and POID in (select POID from Cms_Wsm_POS where AccessoryCompanyNumber = '" + txtAccessoryCompanyNum.Text.Trim() + "')";
            }


            StockBll bll = new StockBll();
            int RecordCount = 0;
            DataTable dt = bll.QueryAccessoryPO(Whereclause, "PODate desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out RecordCount);

            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label POID = e.Item.FindControl("POID") as Label;
            Label POSID = e.Item.FindControl("POSID") as Label;
            Label CheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
            Label IsClose = e.Item.FindControl("lblIsClose") as Label;

            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "delete")
            {

                new SystemBll().Delete<WsmPOEntity>(" and POID = '" + POID.Text + "'");
                new SystemBll().Delete<WsmPOSEntity>(" and POID = '" + POID.Text + "'");
                BindList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblOperatorValue = e.Item.FindControl("lblOperatorValue") as Label;
                Label lblOperator = e.Item.FindControl("lblOperator") as Label;
                LinkButton ltnDel = e.Item.FindControl("ltnDel") as LinkButton;
                LinkButton ltnCheck = e.Item.FindControl("ltnCheck") as LinkButton;
                LinkButton ltnClose = e.Item.FindControl("ltnClose") as LinkButton;
                Label CheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
                Label POID = e.Item.FindControl("POID") as Label;

                if (lblOperator != null && lblOperatorValue != null)
                {
                    SysUserEntity entity = new SysUserBll().Get<SysUserEntity>(new Guid(lblOperatorValue.Text));
                    lblOperator.Text = entity.UserName;
                }

                
            }
        } 
    }
}
